#include "gui.h"


int my_delay(){
    time_t delay = 1; // 3 sec.
    time_t later = time(NULL) + delay;
    while (time(NULL) != later);
}

int get_random( int element, int one, int two, int more ){
    int tmp = 0, seed = 0;
    if ( more == 0 ){
         seed = time(NULL);
    }else{
         seed = time(NULL) + more; 
    }
    //printf("time= %d", seed);
    srand(seed);
    tmp = (rand() % (element-one)) + two;
    return tmp;
}

int PathInGraph( int nbtrajet, int i, int j, t_chemin** t ){
    int a = 0, temp = 0;
    for ( a=0; a<nbtrajet; a++ ){
        if ( t[a]->villedepart == i+1 && t[a]->villearrivee == j+1 ){
             temp = t[a]->distance;
        }
    }
    return temp;
}

void add2tab( t_floyd *myfloyd, int cc, int value ){
    int i = 0;
    while ( myfloyd->ch[cc][i] != -1 ){ i++; }
    myfloyd->ch[cc][i] = value; 
    
    //printf("%d %d %d\n", cc, value, i);  
}

void add2escale( int value, t_avion *avion ){
    int i = 0;
    while ( avion->escale[i] != -1 ){ i++; }
    avion->escale[i] = value;   
}

void DisplayLanes( int aer, t_floyd *myfloyd, int all, int d, int f, int start){
    int i=0, j=0;
    
    if ( all == 1 ){
        for ( i=0; i<aer; i++ ){
             for ( j=0; j<aer; j++ ){
                 if ( i == j ){
                     //printf("%d", i); 
                     printf("");
                 }else if ( myfloyd->key[i][j] == -1 ){
                     printf("Chemin impossible: %d %d", i, j);  
                 }else{
                     DisplayLanes(aer, myfloyd, 0, i, myfloyd->key[i][j], start);
                     //printf("%d", j); 
                     
                     add2tab( myfloyd, start, j ); 
                     start += 1;
                     //printf("\n"); 
                 }
             }
        }
    }else if( all == 0 ){
                 if ( d == f ){
                     //printf("%d", d); 
                     add2tab( myfloyd, start, d );
                 }else if ( myfloyd->key[d][f] == -1 ){
                     printf("Chemin impossible: %d %d", d, f);  
                 }else{
                     DisplayLanes(aer, myfloyd, 0, d, myfloyd->key[d][f], start);
                     //printf("%d", f);
                     add2tab( myfloyd, start, f );  
                 }
    } 
}

t_floyd * PairsShort_FloydWarshall( int nbtrajet, int aer, t_chemin** t ){
    int k = 0, i = 0, j = 0, ch = 0, c = 0;
    
    t_floyd *myfloyd = (t_floyd*)malloc(sizeof(t_floyd));
    printf("Using Floyd-Warshall...\n");

    for ( c=0; c<AA; c++ ){
        for ( i=0; i<BB; i++ ){
           myfloyd->ch[c][i] = -1;
        }
    }
    
    for ( c=0; c<CC; c++ ){
        for ( i=0; i<CC; i++ ){
           myfloyd->key[c][i] = -1;
        }
    }
 
    for ( i=0; i<aer; i++ ){
         for ( j=0; j<aer; j++ ){
                 if ( i == j ){
                      myfloyd->D[i][j] = 0; 
                      myfloyd->key[i][j] = -1;
                 }else if ( (ch = PathInGraph(nbtrajet,i,j,t)) != 0 ){
                      myfloyd->D[i][j] = ch;
                      myfloyd->key[i][j] = i; 
                 }else{
                      myfloyd->D[i][j] = 999999999; 
                      myfloyd->key[i][j] = -1;
                 }
         }
    }
    
    for ( k=0; k<aer; k++ ){
        for ( i=0; i<aer; i++ ){
            for ( j=0; j<aer; j++ ){
                 if ( myfloyd->D[i][k] + myfloyd->D[k][j] < myfloyd->D[i][j] ){
                       myfloyd->D[i][j] = myfloyd->D[i][k] + myfloyd->D[k][j];
                       myfloyd->key[i][j] = myfloyd->key[k][j];
                 }
            }
        }
    }
    DisplayLanes(aer, myfloyd, 1, 0, 0, 0);
    return myfloyd;
}

int nbmoinsun( t_avion **tab, int nb ){
    int i=0;
    for ( i=0; i<nb; i++ ){
        if ( tab[i]->station == -1 ){ return 1; }
    } 
    return 0;
}

int countnbmoinsun( t_avion **tab, int nb ){
    int i=0, j=0;
    for ( i=0; i<nb; i++ ){
        if ( tab[i]->station == -1 ){ j++; }
    } 
    return j;
}

int if_fin( t_avion *avion, int value ){
    int i=0;
    while ( avion->escale[i] != -1 ){ i++; }
    if ( avion->escale[i-1] == value ){ return 1; }else{ return 0; }
}

int next( t_avion *avion, int value ){
    int i=0;
    while ( avion->escale[i] != value ){ i++; }
    if ( avion->escale[i] == value ){ return avion->escale[i+1]; }else{ return -1; }
}

void displayescale( t_avion *avion ){
    int i=0; printf("|");
    while ( avion->escale[i] != -1 ){ 
          printf("%d", avion->escale[i]); i++; 
    }printf("| ");
}

void InitSimulation( t_utils *variables, int theargv ){
      variables->temp = NULL;
      variables->temp2 = 0;
      variables->nbaero = 0;
      variables->nbtrajet = 0;
      variables->i = 0;
      variables->j = 0;
      variables->z = 0;
      variables->h = 0;
      variables->g = 0;
      variables->m = 0;
      variables->avions_totals = 0;
      variables->avions_siml = 0;
      variables->seed = 0;
      variables->avions_capacite[0] = 15000;
      variables->avions_capacite[1] = 10000;
      variables->avions_capacite[2] = 8000;
      variables->ut = theargv; /* 200km en une UT en vitesse normale, 100km en boucle d'attente */
      variables->to_decol = 0; 
      variables->if_good_destination = 0; /* 0 = non / 1 = oui */
      variables->possible = NULL;
      variables->flw = NULL;
      variables->somm_decol = 0;
      variables->redec = 0; 
      variables->determine_fin = 0;
      variables->nbut = 0;
      variables->finished = 0;
     
      printf("Initializing simulation...\n");
      /* G��ation des param�res locaux d'un a�oport */
      variables->faero=fopen("aeroport.txt","r");
      fgets(variables->chaine_aero,1000,variables->faero);
      variables->temp=strtok(variables->chaine_aero,";");
      variables->nbaero=atoi(variables->temp);
     
      //allocation du tableau de structure
      variables->aeroport=(t_areoport**)malloc(variables->nbaero*sizeof(t_areoport));
     
      for(variables->i=0;variables->i<variables->nbaero;variables->i++){
            variables->aeroport[variables->i]=NULL;
      }    
      
      printf("Extracting airports...\n"); 
      for(variables->i=0;variables->i<variables->nbaero;variables->i++){
                      variables->aeroport[variables->i]=(t_areoport*)malloc(sizeof(t_areoport));
                      
                      variables->temp=strtok(NULL,";");
                      variables->aeroport[variables->i]->nom_aero=variables->temp;
                      //printf("nom de l'aero : %s\n",aeroport[i]->nom_aero);
                      
                      variables->temp=strtok(NULL,";");
                      variables->temp2=atoi(variables->temp);
                      variables->aeroport[variables->i]->numero=variables->temp2;
                      //printf("numero %d\n",aeroport[i]->numero);
                      
                      
                      variables->temp=strtok(NULL,";");
                      variables->temp2=atoi(variables->temp);
                      variables->aeroport[variables->i]->piste=variables->temp2;
                      //printf("piste %d\n",aeroport[i]->piste);
                      
                      
                      variables->temp=strtok(NULL,";");
                      variables->temp2=atoi(variables->temp);
                      variables->aeroport[variables->i]->place=variables->temp2;
                      variables->avions_totals += variables->aeroport[variables->i]->place;
                      //printf("place %d\n",aeroport[i]->place);
                      
                      
                      variables->temp=strtok(NULL,";");
                      variables->temp2=atoi(variables->temp);
                      variables->aeroport[variables->i]->attente=variables->temp2;
                      //printf("attente %d\n",aeroport[i]->attente);
                      
                      
                      variables->temp=strtok(NULL,";");
                      variables->temp2=atoi(variables->temp);
                      variables->aeroport[variables->i]->acces=variables->temp2;
                      //printf("acces %d\n",aeroport[i]->acces);
                      
                      variables->temp=strtok(NULL,";");
                      variables->temp2=atoi(variables->temp);
                      variables->aeroport[variables->i]->duree=variables->temp2;
                      //printf("duree %d\n",aeroport[i]->duree);
                     
                      
                      variables->temp=strtok(NULL,";");
                      variables->temp2=atoi(variables->temp);
                      variables->aeroport[variables->i]->collision=variables->temp2;
                      //printf("collision %d\n",aeroport[i]->collision);
                     
                      
                      variables->temp=strtok(NULL,":");
                      variables->temp2=atoi(variables->temp);
                      variables->aeroport[variables->i]->boucle=variables->temp2;
                      //printf("boucle %d\n",aeroport[i]->boucle);
                      
                      variables->aeroport[variables->i]->x = rand()%600;
                      variables->aeroport[variables->i]->y = rand()%400;
                      variables->aeroport[variables->i]->pisteused = 0;
                      
                      //printf("fin de la configuration de l'aeroport %s\n\n",aeroport[i]->nom_aero);             
      }
      fclose(variables->faero);
     
      variables->fchem=fopen("chemin.txt","r");
     
      fgets(variables->chaine_trajet,1000,variables->fchem);
      variables->temp=strtok(variables->chaine_trajet,";");
      variables->nbtrajet=atoi(variables->temp);
     
      variables->trajet=(t_chemin**)malloc(variables->nbtrajet*sizeof(t_chemin*));
     
      for(variables->i=0;variables->i<variables->nbtrajet;variables->i++){
                             variables->trajet[variables->i]=NULL;
      }
       
       printf("Extracting air lane...\n");
       for(variables->i=0;variables->i<variables->nbtrajet;variables->i++){
                               variables->trajet[variables->i]=(t_chemin*)malloc(sizeof(t_chemin));
                              
                               variables->temp=strtok(NULL,";");
                               variables->temp2=atoi(variables->temp);
                               variables->trajet[variables->i]->villedepart=variables->temp2;
                               //printf("num aero depart %d\n",trajet[i]->villedepart);
                               
                               variables->temp=strtok(NULL,";");
                               variables->temp2=atoi(variables->temp);
                               variables->trajet[variables->i]->villearrivee=variables->temp2;
                               //printf("num aero arrivee %d\n",trajet[i]->villearrivee);
                               
                               variables->temp=strtok(NULL,":");
                               variables->temp2=atoi(variables->temp);
                               variables->trajet[variables->i]->distance=variables->temp2;
                               //printf("duree du vol %d\n",trajet[i]->distance);
                               //printf("fin du %d trajet\n\n",i);
                               
                              
       }
       fclose(variables->fchem);
       
       /*  R�lisation d'un Floyd sur tous les noeuds pour trouver les ppc  */
       variables->flw = PairsShort_FloydWarshall( variables->nbtrajet, variables->nbaero, variables->trajet );
       
       /*for ( j=0; j<nbaero; j++ ){
            for ( i=0; i<nbaero; i++ ){
               printf("%d %d %d\n", j, i, flw->D[j][i]);
            }
        }*/
       
       
      /* G��ation des avions et de leurs param�res */
      variables->i = 0; variables->avions_totals = variables->nbaero*2;
      variables->avions_siml = get_random( variables->avions_totals, 5, 5, 0 );
      //variables->avions_siml = 1;
      //allocation du tableau de structure
      variables->avion = (t_avion**)malloc(variables->avions_siml*sizeof(t_avion));
     
      for(variables->i=0;variables->i<variables->avions_siml;variables->i++){
            variables->avion[variables->i] = NULL;
      } 
      
      printf("Generating airplanes...\n");
      variables->temp2 = get_random( RAND_MAX, 0, 0, 0 );
      for(variables->i=0; variables->i<variables->avions_siml; variables->i++){
            variables->avion[variables->i] = (t_avion*)malloc(sizeof(t_avion));
            
            variables->avion[variables->i]->identifiant = variables->i+1; 
            
            variables->temp2 += variables->avion[variables->i]->identifiant;
            variables->avion[variables->i]->depart = get_random( variables->nbaero, 1, 1, variables->temp2 );
            do{
                variables->z = 6;
                variables->temp2 += variables->avion[variables->i]->identifiant+1;
                variables->avion[variables->i]->caburant = variables->avions_capacite[get_random( 3, 0, 0, variables->temp2 )];
                for(variables->j=0;variables->j<variables->nbaero;variables->j++){
                     if ( variables->avion[variables->i]->caburant < variables->flw->D[variables->avion[variables->i]->depart][variables->j] ){ variables->z--; }                 
                }                           
            }while ( variables->z == 0 );                 
    
            variables->avion[variables->i]->consommation = variables->avion[variables->i]->caburant;
            variables->avion[variables->i]->x = 0;
            variables->avion[variables->i]->y = 0;
            
            variables->avion[variables->i]->sol_air = 0;
            variables->avion[variables->i]->station = -1;
            variables->avion[variables->i]->piste = -1;
            variables->avion[variables->i]->envol = -1;
            variables->avion[variables->i]->boucle = -1;
            variables->avion[variables->i]->aterrissage = -1;
            variables->avion[variables->i]->attente = -1;
            variables->avion[variables->i]->pistef = 1;
            variables->avion[variables->i]->stationf = -1;
            variables->avion[variables->i]->arrivee = -1;
            
            for(variables->j=0; variables->j<30; variables->j++){ 
                variables->avion[variables->i]->escale[variables->j] = -1; 
            }
            
            //printf("\nAvion num:%d\nDepart: %d\nCarburant: %d\n\n", avion[i]->identifiant, avion[i]->depart, avion[i]->caburant);
            //my_delay();
      }    
}

t_avion** DoSimulation( t_utils *variables ){
          
  while( variables->determine_fin != 1 && variables->nbut != 0){
         if ( variables->ut <= 0 && countnbmoinsun( variables->avion, variables->avions_siml ) == variables->avions_siml ){ variables->determine_fin = 1; }
         //printf("\nUT: %d", variables->ut);
         printf("\n----- UT ------");
         if ( nbmoinsun( variables->avion, variables->avions_siml ) != 0 && variables->ut > 0 ){
                 /* Choix du nombre d'avions �d�oller */
                 if ( (variables->avions_siml - variables->somm_decol) > 1 ){ 
                      variables->to_decol = get_random( (variables->avions_siml - variables->somm_decol), 0, 0, 0 );
                 }else if ( (variables->avions_siml - variables->somm_decol) == 1 ){
                      variables->to_decol = 1; 
                 }
                 variables->somm_decol += variables->to_decol;
                 
                 //printf("   -  Nous avons %d/%d avions pret a decoler - ", variables->to_decol, variables->avions_siml);
                 variables->dcol_avion = (t_avion**)malloc(variables->to_decol*sizeof(t_avion));
                 variables->j = 0;
                 for(variables->i=0; variables->i<variables->avions_siml; variables->i++){
                     if ( variables->avion[variables->i]->station == -1 && variables->j<variables->to_decol ){
                         variables->dcol_avion[variables->j] = variables->avion[variables->i];   
                         variables->j++;
                     }      
                 }
                    
                 variables->temp2 = get_random( RAND_MAX, 0, 0, 0 );
                 /* D�erminer destinations */
                 for(variables->i=0; variables->i<variables->to_decol; variables->i++){
                     if ( variables->dcol_avion[variables->i]->station == -1 ){ 
                     
                             //for(variables->j=0; variables->j<30; variables->j ++){ variables->dcol_avion[variables->i]->escale[variables->j] = -1; }
                             
                             // Cr�r tableau des choix possibles
                             variables->possible = (int*)malloc((variables->nbaero-1)*sizeof(int));
                             variables->j = 0;
                             for(variables->z=0; variables->z<variables->nbaero; variables->z++){
                                   if ( variables->dcol_avion[variables->i]->depart != variables->z ) { variables->possible[variables->j] = variables->z; variables->j++;}
                             }
                             
                             /*Pour voir la liste des destinations possibles
                             printf("\n%d,%d,", to_decol, dcol_avion[i]->depart);
                             for(z=0; z<j; z++){ printf("%d,", possible[z]); }*/
                
                             variables->dcol_avion[variables->i]->arrivee = variables->possible[get_random( variables->j, 1, 1, variables->temp2 )];
                             variables->temp2 += variables->dcol_avion[variables->i]->arrivee+variables->dcol_avion[variables->i]->depart;
                             //printf("%d - %d\n", dcol_avion[i]->depart, dcol_avion[i]->arrivee);
                             
                             if (variables->dcol_avion[variables->i]->arrivee != variables->dcol_avion[variables->i]->depart){                             
                                    /* Appliquer Dijkstra ou Floyd-Warshall pour d�erminer si
                                       destination accessible et trajet 
                                       
                                       Les points �lier sont dcol_avion[i]->depart et 
                                       dcol_avion[i]->arrivee qui sont les num�os des 
                                       a�oports de d�art et arriv�.
                                    */
                                    
                                    for ( variables->z=0; variables->z<AA; variables->z++ ){
                                        if ( variables->flw->ch[variables->z][0] == variables->dcol_avion[variables->i]->depart ){
                                             variables->g = 0;
                                             for ( variables->h=0; variables->h<BB; variables->h++ ){
                                                  if ( variables->flw->ch[variables->z][variables->h] == -1 && variables->g == 0){
                                                       if ( variables->flw->ch[variables->z][variables->h-1] == variables->dcol_avion[variables->i]->arrivee ){
                                                           //printf("Chemin trouve pour avion %d %d.\n", i, dcol_avion[i]->identifiant);
                                                           variables->dcol_avion[variables->i]->arrivee = next( variables->dcol_avion[variables->i], variables->dcol_avion[variables->i]->depart ); 
                                                           
                                                           
                                                           
                                                           if ( (variables->flw->D[variables->dcol_avion[variables->i]->depart][variables->dcol_avion[variables->i]->arrivee]+(variables->aeroport[variables->dcol_avion[variables->i]->depart]->boucle*100)) < variables->dcol_avion[variables->i]->caburant ){
                                                               displayescale(variables->dcol_avion[variables->i]);
                                                               variables->dcol_avion[variables->i]->station = variables->aeroport[variables->dcol_avion[variables->i]->depart]->acces;
                                                           }else{
                                                               for ( variables->m=0; variables->m<ESC; variables->m++ ){ variables->dcol_avion[variables->i]->escale[variables->m] = -1; }  
                                                           }
                                                           
                                                           //printf(" %d %d\n\n", flw->ch[z][h-1], dcol_avion[i]->arrivee);
                                                       }else{
                                                           for ( variables->m=0; variables->m<ESC; variables->m++ ){ variables->dcol_avion[variables->i]->escale[variables->m] = -1; }  
                                                       }
                                                       variables->g++;     
                                                  }else if ( variables->flw->ch[variables->z][variables->h] != -1 ){
                                                       // Ajouter �escale
                                                       add2escale( variables->flw->ch[variables->z][variables->h], variables->dcol_avion[variables->i] );  
                                                       //printf("%d", variables->flw->ch[variables->z][variables->h]);
                                                  }
                                             }//printf(" ");                             
                                        }
                                    }   
                             }
                     }        
                 }free(variables->dcol_avion); 
         }      
         
         /* Management des avions */
         for(variables->i=0;variables->i<variables->nbaero;variables->i++){
             variables->aeroport[variables->i]->pisteused = 0;          
         }
         
         // Les algorithmes
         for(variables->i=0; variables->i<variables->avions_siml; variables->i++){
              printf("\nVol n:%d (%d, %d) -> ", variables->avion[variables->i]->identifiant, variables->avion[variables->i]->depart, variables->avion[variables->i]->arrivee);
              printf("%d %d %d %d %d %d %d %d %d ", variables->avion[variables->i]->station, variables->avion[variables->i]->piste, variables->aeroport[variables->avion[variables->i]->depart]->pisteused, variables->avion[variables->i]->envol, variables->avion[variables->i]->consommation, variables->avion[variables->i]->sol_air, variables->avion[variables->i]->aterrissage, variables->avion[variables->i]->boucle, variables->avion[variables->i]->attente);
              if ( variables->avion[variables->i]->station > 0 ){ 
                   variables->avion[variables->i]->station -= 1;
                   printf("En station"); 
              }else if ( variables->avion[variables->i]->station == 0 && variables->aeroport[variables->avion[variables->i]->depart]->pisteused < variables->aeroport[variables->avion[variables->i]->depart]->piste ){
                   variables->avion[variables->i]->piste = variables->aeroport[variables->avion[variables->i]->depart]->acces;
                   variables->avion[variables->i]->station = -2;
                   variables->aeroport[variables->avion[variables->i]->depart]->pisteused += 1;
                   printf("Bouge vers piste");
              }else if ( variables->avion[variables->i]->piste > 0 ){
                   variables->avion[variables->i]->piste -= 1;
                   printf("En piste");
              }else if ( variables->avion[variables->i]->piste == 0 ){
                   variables->avion[variables->i]->envol = (variables->flw->D[variables->avion[variables->i]->depart][variables->avion[variables->i]->arrivee]/200);
                   variables->avion[variables->i]->consommation -= 200;
                   variables->avion[variables->i]->sol_air = 1;
                   variables->avion[variables->i]->piste = -1;
                   variables->aeroport[variables->avion[variables->i]->depart]->pisteused -= 1; 
                   printf("Va decoller");
              }else if ( variables->avion[variables->i]->envol > 0 ){
                   variables->avion[variables->i]->envol -= 1; 
                   //variables->avion[variables->i]->consommation = (variables->flw->D[variables->avion[variables->i]->depart][variables->avion[variables->i]->arrivee]/200) - variables->avion[variables->i]->envol;
                   variables->avion[variables->i]->consommation -= 200;
                   
                   //variables->avion[variables->i]->x = variables->avion[variables->i]->consommation*200;
                   variables->avion[variables->i]->x = variables->avion[variables->i]->caburant - variables->avion[variables->i]->consommation;
                   variables->avion[variables->i]->y = (variables->avion[variables->i]->x*100)/variables->avion[variables->i]->caburant;
                   printf("En vol %dpc [%d/%d]", variables->avion[variables->i]->y, variables->avion[variables->i]->x, variables->avion[variables->i]->caburant);
                   
              }else if ( variables->avion[variables->i]->envol == 0 ){
                   if ( variables->aeroport[variables->avion[variables->i]->arrivee]->pisteused < variables->aeroport[variables->avion[variables->i]->arrivee]->piste ) {
                        variables->aeroport[variables->avion[variables->i]->arrivee]->pisteused += 1;
                        variables->avion[variables->i]->aterrissage = variables->aeroport[variables->avion[variables->i]->arrivee]->duree - 1;
                        printf("Va atterir");
                   }else if ( variables->aeroport[variables->avion[variables->i]->arrivee]->pisteused == variables->aeroport[variables->avion[variables->i]->arrivee]->piste ) {
                        if ( variables->avion[variables->i]->consommation - (variables->aeroport[variables->avion[variables->i]->arrivee]->boucle*100) > 0 ){
                             variables->avion[variables->i]->boucle = variables->aeroport[variables->avion[variables->i]->arrivee]->boucle - 1;
                             printf("Va boucler");
                        }
                   }
                   variables->avion[variables->i]->envol = -1;
              }else if ( variables->avion[variables->i]->boucle > 0 ){
                   variables->avion[variables->i]->boucle -= 1; 
                   variables->avion[variables->i]->consommation -= 100;
                   
                   printf("En boucle");
              }else if ( variables->avion[variables->i]->boucle == 0 ){
                   variables->aeroport[variables->avion[variables->i]->arrivee]->pisteused += 1;
                   variables->avion[variables->i]->aterrissage = variables->aeroport[variables->avion[variables->i]->arrivee]->duree - 1; 
                   variables->avion[variables->i]->boucle = -1; 
                   printf("Va atterir");
              }else if ( variables->avion[variables->i]->aterrissage > 0 ){
                   variables->avion[variables->i]->aterrissage -= 1; printf("En atterissage");
              }else if ( variables->avion[variables->i]->aterrissage == 0 && variables->aeroport[variables->avion[variables->i]->arrivee]->pisteused < variables->aeroport[variables->avion[variables->i]->arrivee]->piste ){
                   
                   if ( if_fin( variables->avion[variables->i], variables->avion[variables->i]->arrivee ) ){printf("-2");
                         variables->aeroport[variables->avion[variables->i]->arrivee]->pisteused -= 1;
                         variables->avion[variables->i]->station = -1;
                         variables->avion[variables->i]->consommation = variables->avion[variables->i]->caburant; 
                         variables->avion[variables->i]->x = 0;
                         variables->avion[variables->i]->y = 0;
                         variables->avion[variables->i]->depart = variables->avion[variables->i]->arrivee;
                         variables->avion[variables->i]->attente = variables->aeroport[variables->avion[variables->i]->depart]->attente;
                         printf("Part en station");
                         variables->avion[variables->i]->aterrissage = -1;
                   }else{
                         variables->avion[variables->i]->consommation = variables->avion[variables->i]->caburant;
                         variables->redec = 1; 
                         variables->avion[variables->i]->depart = variables->avion[variables->i]->arrivee;
                         variables->avion[variables->i]->arrivee = next( variables->avion[variables->i], variables->avion[variables->i]->depart );
                         variables->avion[variables->i]->attente = variables->aeroport[variables->avion[variables->i]->depart]->attente;
                         
                         if ( variables->avion[variables->i]->arrivee > -1 && variables->avion[variables->i]->arrivee < variables->nbaero ){
                               printf("Va redecoller");
                               variables->avion[variables->i]->aterrissage = -1;
                         }else{
                               variables->avion[variables->i]->arrivee = variables->avion[variables->i]->depart;
                               variables->aeroport[variables->avion[variables->i]->arrivee]->pisteused -= 1;
                               variables->avion[variables->i]->station = -1;
                               variables->avion[variables->i]->consommation = variables->avion[variables->i]->caburant; 
                               variables->avion[variables->i]->x = 0;
                               variables->avion[variables->i]->y = 0;
                               variables->avion[variables->i]->depart = variables->avion[variables->i]->arrivee;
                               variables->avion[variables->i]->attente = variables->aeroport[variables->avion[variables->i]->depart]->attente;
                               printf("Aeroport inaccessible, on gare");
                               variables->avion[variables->i]->aterrissage = -1;
                         }
                   }                  
                    
                   variables->avion[variables->i]->sol_air = 0;
              }else if ( variables->avion[variables->i]->aterrissage == 0 && variables->aeroport[variables->avion[variables->i]->arrivee]->pisteused == variables->aeroport[variables->avion[variables->i]->arrivee]->piste ){
                   if ( variables->avion[variables->i]->consommation - (variables->aeroport[variables->avion[variables->i]->arrivee]->boucle*100) > 0 ){
                             variables->avion[variables->i]->boucle = variables->aeroport[variables->avion[variables->i]->arrivee]->boucle - 1;
                             printf("Va boucler");
                   } 
              }else if ( variables->avion[variables->i]->attente > 0 ){
                   variables->avion[variables->i]->attente -= 1; 
                   printf("En attente");
              }else if ( variables->avion[variables->i]->attente == 0 ){
                   if ( variables->redec == 1 ){
                        variables->redec = 0;
                        variables->avion[variables->i]->envol = (variables->flw->D[variables->avion[variables->i]->depart][variables->avion[variables->i]->arrivee]/200);
                        printf("Redecolle");
                   }else{ variables->avion[variables->i]->station = -1; } 
                   variables->avion[variables->i]->attente = -1;
              }   
         }printf("\n");       
         //my_delay();
         variables->ut--;
         variables->nbut--;
  } 
  return variables->avion; 
}